const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.feedback = this.model('store/feedback');
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {
			type: 1
		};
		if(!think.isEmpty(posts.keyword)){
			where.content = ['LIKE','%'+posts.keyword+'%'];
		}
		if(!think.isEmpty(posts.userid)){
			where.user_id = posts.userid;
		}
		const data = await this.feedback.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}
	
	async replyAction() {
		try {
			if(this.isGet) {
				let gets = this.get();

				let feedback = await this.feedback.findOne(gets.id);
				this.assign("type", gets.type || 0);
				this.assign("feedback", feedback || {});
				return this.display();
			}
			if(this.isPost) {

				let posts = this.post();
			
			let user = await this.session("user");
					let replyid = await this.feedback.add({
						parent_id: posts.id,
						user_id: user.id,
						type: 0,						
						content: posts.content,
						status: 1,
						create_time: new Date().valueOf()
					});
					if(!think.isEmpty(replyid)) {
						await this.feedback.setOne(posts.id, {
							status: 1
						});
					}
					return this.success("发送成功");

				

			}
		} catch(e) {
			console.log(e)
		}
	}
	async replyListAction() {
		try {

			let gets = this.get();
			let where = {};
			if(!think.isEmpty(gets.parent_id)) {
				where.parent_id = gets.parent_id;
			}

			let feedbacks = await this.feedback.where(where).order({
				create_time: 'ASC'
			}).page(gets.pageNum, gets.pageSize || 5).countSelect();
			console.log(feedbacks)
			return this.json(feedbacks);
		} catch(e) {
			console.log(e)
		}
	}
	
};